﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RouterControl.IO;
using Microsoft.HomeServer.Common.Client;
using RouterControl.Whs.SingletonService;

namespace RouterControl.WhsAddIn.SettingsControls
{
	public partial class UpdateStatus : SettingsControlBase
	{
        public UpdateStatus(RouterControlSettingsTab2 tab)
			: base(tab)
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Initialization start");

			try
			{
				InitializeComponent();
	
				this.lineBox.Header.Text = Properties.Resources.txtUpdateStatus;
                this.updateAvailableLabel.Text = Properties.Resources.txtLoading;
	
				// done
				if (_log.IsDebugEnabled)
					_log.Debug("Initialization done");
			}
			catch (Exception e)
			{
				_log.Fatal("Failed to initialize", e);

				throw;
			}
		}

		private void UpdateUpdateCheckStatus()
		{
// 			if (_log.IsDebugEnabled)
// 				_log.Debug("Updating update status...");

			try
			{
				var newVersion = _singletonService.Channel.GetNewerVersion();
				var lastCheck = _singletonService.Channel.GetLastUpdateCheck();

                if (newVersion != null)
                {
                    this.updateAvailableLabel.Text = string.Format(
                        Properties.Resources.txtUpdateAvailableYesMsg,
                        (lastCheck == DateTime.MinValue ? Properties.Resources.txtUpdateAvailableNeverChecked : lastCheck.ToString()),
                        newVersion.Value.VersionStr
                        );

                    this.statusImage.Image = CommonImages.StatusAtRisk24;
                }
                else
                {
                    this.updateAvailableLabel.Text = string.Format(
                        Properties.Resources.txtUpdateAvailableNoMsg,
                        (lastCheck == DateTime.MinValue ? Properties.Resources.txtUpdateAvailableNeverChecked : lastCheck.ToString()),
                        string.Empty
                        );

                    this.statusImage.Image = CommonImages.StatusHealthy24;
                }
	
			}
			catch (Exception ex)
			{
				_log.Error("Failed to update update status", ex);
			}
		}

		private void updateAvailableCheckTimer_Tick(object sender, EventArgs e)
		{
            if (_singletonService.IsWindowsServiceRunning() != SingletonService2Factory.ServiceStatus.Running)
                return;

			UpdateUpdateCheckStatus();
		}

        private void updateAvailableCheckLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                _singletonService.Channel.CheckNewerVersionNow();

                UpdateUpdateCheckStatus();
            }
            catch (Exception ex)
            {
                _log.Error("Failed to check for update manually", ex);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    ex.Message,
                    Properties.Resources.txtManualUpdateSearchFailed,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void UpdateStatus_Load(object sender, EventArgs e)
        {
            UpdateUpdateCheckStatus();

            this.updateAvailableCheckTimer.Enabled = true;
        }

	}
}
